<template>
  <el-dialog :show-close="false" title="收货地址" :visible="dialogFormVisible">
    <el-form
      :rules="rules"
      ref="form"
      @close="close"
      :model="form"
      :label-width="formLabelWidth"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:dialogFormVisible', false)"
        >取 消</el-button
      >
      <el-button type="primary" @click="handleClick">确 定</el-button>
    </div>
    <div slot="title" class="title">
      <p>添加部门</p>
      <span
        class="el-icon-close"
        @click="$emit('update:dialogFormVisible', false)"
      />
    </div>
  </el-dialog>
</template>
<script>
import { addRolesApi, editRolesApi } from '@/api/settings'
export default {
  name: '',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    row: {
      handler: function(newVal, oldVal) {
        this.form = { ...newVal, permIds: [] }
      },
      immediate: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        description: ''
      },
      formLabelWidth: '120px',
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {},
  computed: {},
  methods: {
    handleClick() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return this.$message.error('请输入内容')
        //  发请求
        this.isEdit ? this.doEdit() : this.doAdd()
      })
      this.$emit('update:dialogFormVisible', false)
    },
    close() {
      this.$emit('update:dialogFormVisible', false)
    },
    async doAdd() {
      /* 发送请求的函数 */
      const res = await addRolesApi(this.form)
      this.$emit('success')
      //  清空表单数据
     
      this.$refs.form.resetFields()
    },
    async doEdit() {
      console.log('edit')
      const res = await editRolesApi(this.form)
      this.$emit('success')
      //  清空表单数据
     
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang='scss'  scoped>
.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
